package office.service.person.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.beanutils.BeanUtils;
import office.action.person.form.LeaveRequestForm;
import office.action.person.form.TripForm;
import office.action.person.vo.PersonLogVo;
import office.action.person.vo.StatisticsVo;
import office.bo.hr.impl.Timing;
import office.bo.person.LeaderInstructions;
import office.bo.person.LeaveRequest;
import office.bo.person.OverTime;
import office.bo.person.PersonLog;
import office.bo.person.Plan;
import office.bo.person.PlanTemplate;
import office.bo.person.Table;
import office.bo.person.Trip;
import office.dao.hr.TimingDP;
import office.dao.person.LeaderInstructionsDP;
import office.dao.person.LeaveRequestDP;
import office.dao.person.OverTimeDP;
import office.dao.person.PersonLogDP;
import office.dao.person.PersonPlanDP;
import office.dao.person.PlanTemplateDP;
import office.dao.person.TableDP;
import office.dao.person.TripDP;
import office.domainset.BizDomainProviderSetImpl;
import office.service.person.IPersonService;
import office.service.system.ISystemService;
import platform.common.bo.Page;
import platform.common.dao.IDomainProvider;
import platform.common.dao.impl.DaoConfigException;
import platform.common.dao.impl.DaoException;
import platform.common.exception.BusinessException;
import platform.common.service.impl.BaseBizServiceImpl;
import platform.util.DateUtil;
import platform.util.Util;

public class PersonServiceImpl extends BaseBizServiceImpl implements IPersonService{
	@Resource
	private ISystemService systemService;
	
	public void saveLeaderInstructions(LeaderInstructions leaderInstructions){
		try {
			getDP(LeaderInstructionsDP.class).saveOrUpdate(leaderInstructions);
		} catch (DaoException e) {
			log.error("", e);
		}
	}
	
	public void savePersonLog(PersonLog personLog){
		try {
			getDP(PersonLogDP.class).saveOrUpdate(personLog);
		} catch (DaoException e) {
			log.error("", e);
		}
	}
	
	@SuppressWarnings("unchecked")
	public void saveTiming(Timing timing){
		try {
			Object params[] = {timing.getUser().getId(),timing.getThisDate(),timing.getType(),timing.getTrip().getId()};
			List<Timing> list = getDP(TimingDP.class).findByHqlNameAndDomian("validateTiming",params);
			if(list == null || list.size() == 0){
				getDP(TimingDP.class).saveOrUpdate(timing);
				getDP(TimingDP.class).getMySession().flush();
			}
		} catch (Exception e) {
			log.error("", e);
		}
	}
	@SuppressWarnings("unchecked")
	public List<PersonLog> getPersonLogByUid(String uid){
		String params[] = {uid};
		try {
			List<PersonLog> list = getDP(PersonLogDP.class).findByHqlNameAndDomian("logByUid",params);
			return list;
		} catch (Exception e) {
			log.error("", e);
			return null;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<PersonLog> getPersonLogByUidAndDate(Page page,String uid,Integer logDay){
		try {
			PersonLogDP personLogDP = getDP(PersonLogDP.class);
			Map map = new HashMap();
			map.put("user.id", uid);
			map.put("logDayStart", DateUtil.getDateStart(logDay));
			map.put("logDayEnd", DateUtil.getDateStart(-1));
			List<PersonLog> list = personLogDP.findByDynamicHql("getLogByParams",map,new HashMap(),page);
			return list;
		} catch (Exception e) {
			log.error("", e);
			return null;
		}
	}

	@Override
	public void saveLeaveRequest(LeaveRequest leaveRequest) throws BusinessException{
		try {
			//modify by xlp
			if(null==leaveRequest.getId() || ("").equals(leaveRequest.getId())){
				LeaveRequest bo = getDP(LeaveRequestDP.class).newEmptyObj();
				BeanUtils.copyProperties(bo,leaveRequest);
				Calendar c = Calendar.getInstance();
				SimpleDateFormat dateFmt = new SimpleDateFormat("yyyy-MM-dd");
				bo.setEntryDate(dateFmt.format(c.getTime()));
				bo.setState("0");
				bo.store(domainProviderSet);
			}else{
				LeaveRequest bo = getDP(LeaveRequestDP.class).findByIdAndDomain(leaveRequest.getId());
				bo.setBeginDate(leaveRequest.getBeginDate());
				bo.setEndDate(leaveRequest.getEndDate());
				bo.setReson(leaveRequest.getReson());
				bo.setSumDay(leaveRequest.getSumDay());
				bo.store(domainProviderSet);
			}
		} catch (Exception e) {
			log.error("", e);
		}
		
	}

	@Override
	public void saveOverTime(OverTime overTime) {
		try {
			getDP(OverTimeDP.class).saveOrUpdate(overTime);
		} catch (DaoException e) {
			log.error("", e);
		}
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<OverTime> getOverTimeByUid(String userId) {
		Object params[] = {userId};
		List<OverTime> list = null;
		try {
			list = getDP(OverTimeDP.class).findByHqlNameAndDomian("overTimeByUid",params);
		} catch (Exception e) {
			log.error("", e);
		}
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<LeaderInstructions> getLeaderInstructionsByPlanId(String planId){
		Object params[] = {planId};
		List<LeaderInstructions> list = null;
		try {
			list = getDP(LeaderInstructionsDP.class).findByHqlNameAndDomian("leaderInstructionsByPlanId",params);
		} catch (Exception e) {
			log.error("", e);
		}
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<LeaderInstructions> getNoReadLeaderInstructionsList(String planId,String isRead){
		Object params[] = {planId,isRead};
		List<LeaderInstructions> list = null;
		try {
			list = getDP(LeaderInstructionsDP.class).findByHqlNameAndDomian("noReadLeaderInstructionsList",params);
		} catch (Exception e) {
			log.error("", e);
		}
		return list;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<OverTime> getOverTimeByProcessUid(String processUid,Integer status) {
		Object params[] = {processUid,status};
		List<OverTime> list = null;
		try {
			list = getDP(OverTimeDP.class).findByHqlNameAndDomian("overTimeByProcessUid",params);
		} catch (Exception e) {
			log.error("", e);
		}
		return list;
	}
	
	@Override
	public void overTimeProcess(String id,Integer status) throws BusinessException {
		try {
			OverTime overTime = getDP(OverTimeDP.class).findByIdAndDomain(id);
			overTime.setStatus(status);
		} catch (Exception e) {
			throw this.handleBusinessServiceException("根据id修改车辆状态信息失败!", e);
		}
	}

	@Override
	public void saveTrip(Trip trip) throws BusinessException{
		try {
			//modify by xlp
			if(null==trip.getId() || ("").equals(trip.getId())){
				Trip bo = getDP(TripDP.class).newEmptyObj();
				BeanUtils.copyProperties(bo,trip);
				Calendar c = Calendar.getInstance();
				SimpleDateFormat dateFmt = new SimpleDateFormat("yyyy-MM-dd");
				bo.setEntryDate(dateFmt.format(c.getTime()));
				bo.setState("0");
				bo.store(domainProviderSet);
			}else{
				Trip bo = getDP(TripDP.class).findByIdAndDomain(trip.getId());
				bo.setBeginDate(trip.getBeginDate());
				bo.setEndDate(trip.getEndDate());
				bo.setSumTime(trip.getSumTime());
				bo.setReson(trip.getReson());
				bo.setTripMoney(bo.getTripMoney());
				bo.setGiftName(bo.getGiftName());
				bo.store(domainProviderSet);
			}
		} catch (Exception e) {
			throw this.handleBusinessServiceException("编辑出差信息失败!", e);
		}
	}

	@Override
	public void savePlanTemplate(PlanTemplate planTemplate) {
		try {
			getDP(PlanTemplateDP.class).saveOrUpdate(planTemplate);
		} catch (DaoException e) {
			log.error("", e);
		}
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Plan> getPlanByUidAndYear(String userId,Integer makeYear) {
		Object params[] = {userId,makeYear};
		List<Plan> list = null;
		try {
			list = getDP(PersonPlanDP.class).findByHqlNameAndDomian("planByUidAndYear",params);
		} catch (Exception e) {
			log.error("", e);
		}
		return list;
	}

	@Override
	public void savePersonPlan(Plan plan) {
		try {
			BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
			domainProviderSetImpl.getDao().saveOrUpdate(plan);
		} catch (DaoException e) {
			log.error("", e);
		}
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PlanTemplate> getPlanTemplateByUidAndYear(Integer makeYear) {
		BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
		IDomainProvider planTemplateDP = domainProviderSetImpl.getPlanTemplateDP();
		Object params[] = {makeYear};
		List<PlanTemplate> list = null;
		try {
			list = (List<PlanTemplate>)domainProviderSetImpl.getDao().findByHqlNameAndDomian(planTemplateDP,"planTemplateByYear",params);
		} catch (Exception e) {
			log.error("", e);
		}
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<Timing> getAllTimingByUid(String uid,String beginDate,String endDate)throws BusinessException{
		BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
		IDomainProvider timingDP = domainProviderSetImpl.getTimingDP();
		if(beginDate != null && endDate != null){
			Object params[] = {uid,beginDate,endDate};
			List<Timing> list = null;
			try {
				list = (List<Timing>)domainProviderSetImpl.getDao().findByHqlNameAndDomian(timingDP,"allTimingByUidAndDate",params);
				return list;
			} catch (DaoException e) {
				e.printStackTrace();
			} catch (DaoConfigException e) {
				e.printStackTrace();
			}
		}else{
			Object params[] = {uid};
			List<Timing> list = null;
			try {
				list = (List<Timing>)domainProviderSetImpl.getDao().findByHqlNameAndDomian(timingDP,"allTimingByUid",params);
				return list;
			} catch (Exception e) {
				log.error("", e);
			}
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<Timing> getAllTimingByDate(String beginDate,String endDate)throws BusinessException{
		BizDomainProviderSetImpl domainProviderSetImpl = (BizDomainProviderSetImpl) domainProviderSet;
		IDomainProvider timingDP = domainProviderSetImpl.getTimingDP();
		if(beginDate != null && endDate != null){
			Object params[] = {beginDate,endDate};
			List<Timing> list = null;
			try {
				list = (List<Timing>)domainProviderSetImpl.getDao().findByHqlNameAndDomian(timingDP,"allTimingByDate",params);
				return list;
			} catch (Exception e) {
				log.error("", e);
			}
		}
		return null;
	}

	@Override
	public PersonLog getPersonLogById(String id) {
		try {
			PersonLogDP personLogDP = getDP(PersonLogDP.class);
			PersonLog person = personLogDP.findByIdAndDomain(id);
			return person;
		} catch (Exception e) {
			log.error("", e);
			return null;
		} 
	}

	@Override
	public void deletePersonLogById(String id) {
		try {
			PersonLogDP personLogDP = getDP(PersonLogDP.class);
			personLogDP.deleteById(personLogDP, id);
		} catch (Exception e) {
			log.error("", e);
		}
	}

	@Override
	public void deleteById(String id) {
		try {
			getDP(TripDP.class).deleteById(id);
		} catch (Exception e) {
			log.error("", e);
		}
	}

	@Override
	public LeaveRequest getLeaveById(String id) throws BusinessException {
		try {
			LeaveRequest leaveRequest = getDP(LeaveRequestDP.class).findByIdAndDomain(id);
			return leaveRequest;
		} catch (Exception e) {
			throw this.handleBusinessServiceException("根据id修改车辆状态信息失败!", e);
		}
	}

	@Override
	public void deleteLeaveById(String id) {
		try {
			getDP(LeaveRequestDP.class).deleteById(id);
		} catch (Exception e) {
			log.error("", e);
		}
	}

	@Override
	public Trip getTripById(String id) throws BusinessException {
		try {
			Trip trip = getDP(TripDP.class).findByIdAndDomain(id);
			return trip;
		} catch (Exception e) {
			throw this.handleBusinessServiceException("根据id修改车辆状态信息失败!", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<LeaveRequest> getLeaveRequestForSum(Integer year,Integer month, Integer status) throws BusinessException {
		try {
			Object params[] = {status+"",DateUtil.getBegindate(year,month),DateUtil.getEnddate(year,month)};
			List<LeaveRequest> leaveRequestList = getDP(LeaveRequestDP.class).findByHqlNameAndDomian("leaveRequestForSum", params);
			return leaveRequestList;
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("根据id修改车辆状态信息失败!", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<OverTime> getOverTimeForSum(Integer year,Integer month, Integer status)
			throws BusinessException {
		try {
			Object params[] = {status,DateUtil.getBegindate(year,month),DateUtil.getEnddate(year,month)};
			List<OverTime> overTimeList = getDP(OverTimeDP.class).findByHqlNameAndDomian("overTimeForSum", params);
			return overTimeList;
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("根据id修改车辆状态信息失败!", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Trip> getTripForSum(Integer year,Integer month, Integer status)
			throws BusinessException {
		try {
			Object params[] = {status+"",DateUtil.getBegindate(year,month),DateUtil.getEnddate(year,month)};
			List<Trip> tripList = getDP(TripDP.class).findByHqlNameAndDomian("tripForSum", params);
			return tripList;
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("根据id修改车辆状态信息失败!", e);
		}
	}

	@Override
	public Plan getPlanById(String id) throws BusinessException {
		try {
			Plan plan =  getDP(PersonPlanDP.class).findByIdAndDomain(id);
			return plan;
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("根据id修改车辆状态信息失败!", e);
		}
	}

	@Override
	public OverTime getOverTimeById(String id) throws BusinessException {
		try {
			OverTime overTime = getDP(OverTimeDP.class).findByIdAndDomain(id);
			return overTime;
		} catch (Exception e) {
			log.error("", e);
			throw this.handleBusinessServiceException("根据id修改车辆状态信息失败!", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public Trip getTripByUidAndDate(String userId) {
		Map <String,Object> map = new HashMap<String,Object>();
		map.put("uid", userId);
		map.put("date", DateUtil.getCurrentDate());
		map.put("sdate", DateUtil.getDate());
		List<Trip> list = null;
		try {
			list = getDP(TripDP.class).findByHqlNameAndDomian("tripByUidAndDate",map);
			if( list != null && list.size() > 0 ){
				return list.get(0);
			}else{
				return null;
			}
		} catch (Exception e) {
			log.error("", e);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Timing> getTimingByTrip(String id) throws BusinessException {
		Object params[] = {id};
		List<Timing> list = null;
		try {
			list = getDP(TimingDP.class).findByHqlNameAndDomian("timingByTrip",params);
			return list;
		} catch (Exception e) {
			log.error("", e);
		}
		return null;
	}
	
	@Override
	public Timing getTimingById(String id) throws BusinessException {
		Timing timing = null;
		try {
			timing = getDP(TimingDP.class).findByIdAndDomain(id);
			return timing;
		} catch (Exception e) {
			log.error("", e);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Timing getTimingByDateAndType(String uid,String date,String clockType) throws BusinessException {
		Object params[] = {uid,date,clockType};
		List<Timing> list = null;
		try {
			list = getDP(TimingDP.class).findByHqlNameAndDomian("timingByDateAndType",params);
			if(list != null && list.size() > 0){
				return list.get(0);
			}
		} catch (Exception e) {
			log.error("", e);
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Table> getTableByUser(String userId, String month) {
		String method = "";
		Object params[] = {userId,month};
		if(month != null && !month.trim().equals("")){
			method = "tableByUserAndDate";
		}else{
			method = "allTableByUser";
		}
		List<Table> list = null;
		try {
			list = getDP(TableDP.class).findByHqlNameAndDomian(method,params);
			return list;
		} catch (Exception e) {
			log.error("", e);
		}
		return null;
	}
	
	@Override
	public void saveTable(Table table) {
		try {
			getDP(TableDP.class).saveOrUpdate(table);
		} catch (DaoException e) {
			log.error("", e);
		}
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<Table> getAllTableByDate(String userId,String beginDate,String endDate) {
		try {
			List<Table> list = null;
			if(userId != null && !userId.trim().equals("")){
				Object params[] = {userId,beginDate,endDate};
				list = getDP(TableDP.class).findByHqlNameAndDomian("allTableByUserAndDate",params);
				return list;
			}else{
				Object params[] = {beginDate,endDate};
				list = getDP(TableDP.class).findByHqlNameAndDomian("allTableByDate",params);
				return list;
			}
		} catch (Exception e) {
			log.error("", e);
		}
		return null;
	}
	@Override
	public Table getTableById(String id) throws BusinessException {
		try {
			Table table = getDP(TableDP.class).findByIdAndDomain(id);
			return table;
		} catch (Exception e) {
			throw this.handleBusinessServiceException("根据id修改车辆状态信息失败!", e);
		}
	}
	@Override
	public void deleteTableById(String id) {
		try {
			getDP(TableDP.class).deleteById(id);
		} catch (Exception e) {
			log.error("", e);
		}
	}
	/**
	 * 获取请假数据列表
	 * @param form
	 * @return
	 * @throws BusinessException
	 */
	@SuppressWarnings("unchecked")
	public List<LeaveRequestForm> getListLeaveRequest(LeaveRequestForm objctForm) throws BusinessException{
		List<LeaveRequestForm> listData = new ArrayList<LeaveRequestForm>();
		try {
			Map map = new HashMap();
			map.put("userId", objctForm.getEntryUserId());
			map.put("checkRole", objctForm.getCheckRole());
			List<Object> deptId = null;
			if(objctForm.getRoleDFlag().equals("0")){
				deptId = systemService.getDescendantsId(objctForm.getDeptId());
			}else if(objctForm.getRoleDFlag().equals("1")){
				deptId = systemService.getDepartment(objctForm.getDeptId(),objctForm.getCompanyId());
			}
			if(null==objctForm.getCheckRole() || ("").equals(objctForm.getCheckRole())){
				throw new BusinessException("用户未设置相关角色!");
			}else{
				map.put("deptId", deptId);
				listData = getDP(LeaveRequestDP.class).findByHqlNameAndDomian("findByUserIdCheckRoleDept",map);
			}
		} catch (Exception e) {
			throw this.handleBusinessServiceException("获取请假列表信息失败!", e);
		}
		return listData;
	}
	
	/**
	 * 根据id获取对象
	 * @param id
	 * @return
	 * @throws BusinessException
	 */
	@SuppressWarnings("unchecked")
	public LeaveRequestForm getLeaveRequestById(String id)throws BusinessException{
		LeaveRequestForm form= new LeaveRequestForm();
		try {
			Map map = new HashMap();
			map.put("id", id);
			List list = getDP(LeaveRequestDP.class).findByHqlNameAndDomian("findById",map);
			if(list.size()>0){
				form = (LeaveRequestForm)list.get(0);
			}
		} catch (Exception e) {
			throw this.handleBusinessServiceException("根据请假id获取对象信息失败!", e);
		}
		return form;
	}
	
	/**
	 * 获取出差数据列表
	 * @param form
	 * @return
	 * @throws BusinessException
	 */
	@SuppressWarnings("unchecked")
	public List<TripForm> getListTrip(TripForm objctForm) throws BusinessException{
		List<TripForm> listData = new ArrayList<TripForm>();
		try {
			Map map = new HashMap();
			map.put("userId", objctForm.getEntryUserId());
			map.put("checkRole", objctForm.getCheckRole());
			List<Object> deptId = null;
			if(objctForm.getRoleDFlag().equals("0")){
				deptId = systemService.getDescendantsId(objctForm.getDeptId());
			}else if(objctForm.getRoleDFlag().equals("1")){
				deptId = systemService.getDepartment(objctForm.getDeptId(),objctForm.getCompanyId());
			}
			if(null==objctForm.getCheckRole() || ("").equals(objctForm.getCheckRole())){
				throw new BusinessException("用户未设置相关角色!");
			}else{
				map.put("deptId", deptId);
				listData = getDP(TripDP.class).findByHqlNameAndDomian("findByUserIdCheckRoleDept",map);
			}
		} catch (Exception e) {
			throw this.handleBusinessServiceException("获取出差列表信息失败!", e);
		}
		return listData;
	}
	/**
	 * 根据id获取对象
	 * @param id
	 * @return
	 * @throws BusinessException
	 */
	@SuppressWarnings("unchecked")
	public TripForm getTripFormById(String id)throws BusinessException{
		TripForm form= new TripForm();
		try {
			Map map = new HashMap();
			map.put("id", id);
			List list = getDP(TripDP.class).findByHqlNameAndDomian("findById",map);
			if(list.size()>0){
				form = (TripForm)list.get(0);
			}
		} catch (Exception e) {
			throw this.handleBusinessServiceException("根据出差id获取对象信息失败!", e);
		}
		return form;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<TripForm> getListTripForFinance(TripForm objctForm)throws BusinessException {
		List<TripForm> listData = new ArrayList<TripForm>();
		try {
			Map map = new HashMap();
			map.put("state", objctForm.getState());
			listData = getDP(TripDP.class).findByHqlNameAndDomian("listTripForFinance",map);
		} catch (Exception e) {
			throw this.handleBusinessServiceException("获取出差列表信息失败!", e);
		}
		return listData;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Page getPersonLogTotalPage(PersonLogVo personLogVo,Integer logDay) {
		Page page = new Page();
		try{
			PersonLogDP personLogDP = getDP(PersonLogDP.class);
			int pageSize = Integer.parseInt(Util.parsePropertiesReturnStr("project", "common.pageSize"));
			page.setPageSize(pageSize);
			Map<String,Object> condMap= new HashMap<String,Object>();
			if(null!=personLogVo&&!Util.isNull(personLogVo.getUser().getId())){
				condMap.put("user.id", personLogVo.getUser().getId());
			}
			if(null!=logDay&&!Util.isNull(logDay)){
				condMap.put("logDayStart", DateUtil.getDateStart(logDay));
				condMap.put("logDayEnd", DateUtil.getDateStart(-1));
			}
			if(null!=personLogVo&&!Util.isNull(personLogVo.getCreateDate())){
				condMap.put("createDate", personLogVo.getCreateDate());
			}
			List<PersonLog> list = personLogDP.findByDynamicHql("getLogByParams", condMap,new HashMap());
			if(list.size()%pageSize == 0){
				page.setTotalPage(list.size()/pageSize);
			}else{
				page.setTotalPage(list.size()/pageSize+1);
			}
		}catch(Exception e){
			log.error("", e);
			this.handleBusinessServiceException("获得分页信息失败", e);
		}	
		return page;
	}
	
	@SuppressWarnings("unchecked")
	public List<TripForm> getAllTrip(Integer year,Integer month, Integer bstate,Integer lstate) throws BusinessException{
		try {
			Map map = new HashMap();
			map.put("beginDate", DateUtil.getBegindate(year,month));
			map.put("endDate", DateUtil.getEnddate(year,month));
			map.put("bstate", bstate);
			map.put("lstate", lstate);
			List<TripForm> tripList = getDP(TripDP.class).findByHqlNameAndDomian("allTrip", map);
			return tripList;
		} catch (Exception e) {
			throw this.handleBusinessServiceException("获取总经理出差列表出错!", e);
		}
	}
	@Override
	public void changeTripStatus(Trip trip) throws BusinessException {
		try {
			Trip newTrip = getDP(TripDP.class).findByIdAndDomain(trip.getId());
			newTrip.setState(trip.getState());
		} catch (Exception e) {
			log.error("", e);
		}
	}
	
	@Override
	public List<StatisticsVo> getHrSum(Integer year,Integer month,String type) throws BusinessException {
		List<StatisticsVo> statisticsVoList = new ArrayList<StatisticsVo>();
		if(type != null && type.equals("trip")){
			List<Trip> tripList = getTripForSum(year,month, 100);
			for (Iterator<Trip> iterator = tripList.iterator(); iterator
					.hasNext();) {
				StatisticsVo statisticsVo = new StatisticsVo();
				Trip trip = (Trip) iterator.next();
				statisticsVo.setBeginDate(trip.getBeginDate());
				statisticsVo.setEndDate(trip.getEndDate());
				statisticsVo.setSumDay(trip.getSumTime());
				statisticsVo.setCreateUserId(trip.getUser().getId());
				statisticsVo.setCreateUserCname(trip.getUser().getCname());
				statisticsVo.setType("trip");
				statisticsVoList.add(statisticsVo);
			}
		}else if(type != null && type.equals("leavel")){
			List<LeaveRequest> leaveRequestList = getLeaveRequestForSum(year,month, 100);
			for (Iterator<LeaveRequest> iterator = leaveRequestList.iterator(); iterator.hasNext();) {
				StatisticsVo statisticsVo = new StatisticsVo();
				LeaveRequest leaveRequest = (LeaveRequest) iterator.next();
				statisticsVo.setBeginDate(leaveRequest.getBeginDate());
				statisticsVo.setEndDate(leaveRequest.getEndDate());
				statisticsVo.setSumDay(leaveRequest.getSumDay());
				statisticsVo.setCreateUserId(leaveRequest.getUser().getId());
				statisticsVo.setCreateUserCname(leaveRequest.getUser().getCname());
				statisticsVo.setType("leave");
				statisticsVoList.add(statisticsVo);
			}
		}else if(type != null && type.equals("overtime")){
			List<OverTime> overTimeList = getOverTimeForSum(year,month, 100);
			for (Iterator<OverTime> iterator = overTimeList.iterator(); iterator.hasNext();) {
				StatisticsVo statisticsVo = new StatisticsVo();
				OverTime overTime = (OverTime) iterator.next();
				statisticsVo.setBeginDate(overTime.getBeginDate());
				statisticsVo.setEndDate(overTime.getEndDate());
				statisticsVo.setSumDay(overTime.getSumTime());
				statisticsVo.setCreateUserId(overTime.getUser().getId());
				statisticsVo.setCreateUserCname(overTime.getUser().getCname());
				statisticsVo.setType("overtime");
				statisticsVoList.add(statisticsVo);
			}
		}
		return statisticsVoList;
	}
	@Override
	public void deleteOverTimeById(String id) {
		try {
			getDP(OverTimeDP.class).deleteById(id);
		} catch (Exception e) {
			log.error("", e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<TripForm> validateTrip(String uid,String beginDate)throws BusinessException{
		try {
			Map map = new HashMap();
			map.put("userId", uid);
			map.put("beginDate", beginDate);
			List<TripForm> tripList = getDP(TripDP.class).findByHqlNameAndDomian("validateTrip", map);
			return tripList;
		} catch (Exception e) {
			throw this.handleBusinessServiceException("获取总经理出差列表出错!", e);
		}
	}
	
	public ISystemService getSystemService() {
		return systemService;
	}
	public void setSystemService(ISystemService systemService) {
		this.systemService = systemService;
	}

	@Override
	public void deleteTimingByTripAndDate(String tripId,String thisDate) {
		Map<String,Object> dmap = new HashMap<String,Object>();
		dmap.put("thisDate", thisDate);
		Map<String,Object> hmap = new HashMap<String,Object>();
		hmap.put("tripId", tripId);
		try {
			getDP(TimingDP.class).exeDynamicHql("deleteTimingByTripAndDate", dmap, hmap);
		} catch (Exception e) {
			log.error("",e);
		}
	}
}
